package cate.game.play.skill.passive.hero;

import cate.game.attr.FightAttr;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * 敌方每携带一个【茶香印记】提升龙井虾仁3%攻击力，最高叠加10层，印记不可被驱散！
 * 携带3个【茶香印记】的目标受到的伤害增加30%
 */
public class 随君终不悔PH extends PassiveHandler {

	private String buff标签;

	private FightAttr 属性提升;

	private int 提升最大层数;

	private int buff层数;

	private FightAttr 敌方属性;

	//buff标签=？？&属性提升=4:300&提升最大层数=10
	//&buff层数=3&敌方属性=24:-3000
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		buff标签 = args.get("buff标签");
		属性提升 = new FightAttr(args.get("属性提升"));
		提升最大层数 = args.getInt("提升最大层数", 0);

		buff层数 = args.getInt("buff层数", 0);
		敌方属性 = new FightAttr(args.get("敌方属性"));
	}

	private Map<Integer,Integer> buffNumMap = new HashMap<>();

	@Override
	public void onMoveDone(MoveCtx move, TimeActionCtx action) {
		int curTotal = 0;
		for (Fighter enemy : move.getTeamVS().getEnemyTeam(skill.owner.getTeam()).getAreaFighters()) {
			int curNum = enemy.buff.countBuff(b -> b.caster.getPid() == skill.owner.getPid() && b.cfg.containTag(buff标签));
			curTotal+=curNum;
			int recNum = buffNumMap.getOrDefault(enemy.getPid(), 0);
			if (curNum == recNum) {
				continue;
			}
			if (curNum >= buff层数 && recNum < buff层数) {
				enemy.attr.addAttr(敌方属性);
			}
			if (curNum < buff层数 && recNum >= buff层数) {
				enemy.attr.addAttr(敌方属性, false);
			}
			buffNumMap.put(enemy.getPid(), curNum);
		}
		int oldTotal = Math.max(提升最大层数, buffNumMap.values().stream().mapToInt(a -> a).sum());
		curTotal = Math.max(提升最大层数, curTotal);
		if (oldTotal != curTotal) {
			skill.owner.attr.addAttr(属性提升.copy().scale(curTotal - oldTotal));
		}
	}
}
